package com.txsj.scan.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.txsj.scan.R;
import com.txsj.scan.bean.LabelBean;
import com.txsj.scan.utils.LabelUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @Created by WangChuangJun on 2018/12/7
 * @Describe
 */
public class UnbindLabelAdapter extends RecyclerView.Adapter<UnbindLabelAdapter.EpcViewHolder> {
    private Context context;
    private List<LabelBean> labelBeanList;

    public UnbindLabelAdapter(Context context) {
        this.context = context;
    }

    public List<LabelBean> getLabelBeanList() {
        return labelBeanList;
    }

    public void setLabelBeanList(List<LabelBean> list){
        this.labelBeanList = list;
        Collections.sort(labelBeanList, new Comparator<LabelBean>() {
            @Override
            public int compare(LabelBean o1, LabelBean o2) {
                if (o1.getGroupId() == o2.getGroupId()){
                    return (int) (o1.getSerialGroupId() - o2.getSerialGroupId());
                }
                return (int) (o1.getGroupId() - o2.getGroupId());
            }
        });
        notifyDataSetChanged();
    }

    @Override
    public EpcViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new EpcViewHolder(LayoutInflater.from(context).inflate(R.layout.item_epc, parent, false));
    }

    @Override
    public void onBindViewHolder(EpcViewHolder holder, int position) {
        holder.bind(labelBeanList.get(position).getStrEpc(), position);
    }

    @Override
    public int getItemCount() {
        return labelBeanList.size();
    }

    /**
     * 移除
     * @param labelBean
     */
    public void removeData(LabelBean labelBean) {
        boolean isContain = false;
        int position = -1;
        for (int i = 0;i<labelBeanList.size();i++){
            if (labelBean.getStrEpc().equals(labelBeanList.get(i).getStrEpc())){
                isContain = true;
                position = i;
                break;
            }
        }
        if (isContain){
            labelBeanList.remove(labelBeanList.get(position));
        }
        notifyDataSetChanged();

    }

//    /**
//     * 重新添加
//     * @param labelBean1
//     */
//    public void reAddData(LabelBean labelBean1) {
//        if (!labelBeanList.contains(labelBean1)) {
//            labelBeanList.add(labelBean1);
//            notifyDataSetChanged();
//        }
//    }

    public class EpcViewHolder extends RecyclerView.ViewHolder {

        View rootView;
        TextView tv_classify_num, tv_group_num, tv_pipeline_num;

        public EpcViewHolder(View itemView) {
            super(itemView);
            this.rootView = itemView;
            tv_classify_num = (TextView) itemView.findViewById(R.id.tv_classify_num);
            tv_group_num = (TextView) itemView.findViewById(R.id.tv_group_num);
            tv_pipeline_num = (TextView) itemView.findViewById(R.id.tv_pipeline_num);
        }

        public void bind(String t, final int position) {
            if (position % 2 == 0) {
                rootView.setBackground(context.getResources().getDrawable(R.drawable.click_grey_bg));
            } else {
                rootView.setBackground(context.getResources().getDrawable(R.drawable.click_white_bg));
            }

            rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != listener)listener.onClick(position);
                }
            });

            String strEpc = t;//此时的strEpc已经没有了空格
//            //解析
//            String epcCode = strEpc.substring(9, strEpc.length());
//            //场景调节数
//            long sceneNumber = Integer.parseInt(epcCode.substring(0, 4), 16);
//            //分类ID
//            long classId = Integer.parseInt(epcCode.substring(4, 6), 16);
//            //流水号
//            long serialNumber = Integer.parseInt(epcCode.substring(6, epcCode.length()), 16);
//            //分组标签数量
//            long groupTagNumber = (long) Math.pow(2, classId);
//            //当前场景下的每组标签数量
//            long sceneGroupTagNumber = groupTagNumber - sceneNumber;
//            //分组id
//            long groupId = (serialNumber / groupTagNumber) + 1;
//            //m_groupSerialNumber = (long)(SerialNumber % GroupTagNumber) + 1;
//            //当前场景下的流水号
//            long serialTagNumber = serialNumber % groupTagNumber + 1;

            tv_classify_num.setText(LabelUtils.getClassId(strEpc) + "");
            tv_group_num.setText(LabelUtils.getGroupId(strEpc) + "");
            tv_pipeline_num.setText(LabelUtils.getSceneGroupTagNumber(strEpc));
        }
    }

    private OnItemClickListener listener;

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener{
        void onClick(int position);
    }
}
